package com.moba.security;

import com.moba.domain.UserInfo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.List;
import java.util.stream.Collectors;

public final class JwtUserFactory {

  private JwtUserFactory() {}

  public static JwtUser create(UserInfo user) {
    return new JwtUser(
        user.getId().toString(),
        user.getUserName(),
        user.getPassword(),
        mapToGrantedAuthorities(user.getAuthorities()));
  }

  private static List<GrantedAuthority> mapToGrantedAuthorities(List<String> authorities) {
    return authorities.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
  }
}
